﻿package com.xeem.henkel.st
{
	import com.adobe.images.*;
	import com.greensock.*;
	import com.greensock.easing.*;
	import com.xeem.core.utils.*;
	import com.xeem.henkel.common.*;

	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.net.FileReference;
	import flash.printing.*;
	import flash.text.*;


	import org.alivepdf.display.Display;
	import org.alivepdf.layout.Mode;
	import org.alivepdf.layout.Orientation;
	import org.alivepdf.layout.Position;
	import org.alivepdf.layout.Resize;
	import org.alivepdf.layout.Unit;
	import org.alivepdf.pdf.PDF;
	import org.alivepdf.saving.Method;
	import org.alivepdf.images.ImageFormat;

	public class Main extends MovieClip
	{
		private var styler:NumberFormat=new NumberFormat();
		private var _bar0:Sprite;
		private var _bar1:Sprite;
		private var _bar2:Sprite;
		private var _bar3:Sprite;
		private var _bar4:Sprite;

		public function Main()
		{
			init();
			trace("Hello");
		}

		private function init()
		{

			var tf:TextFormat = new TextFormat();
			tf.size = 12;
			//tf.bold = true;
			tf.font = "Arial";
			//tf.color=0x008CD6;
			var tf1:TextFormat = new TextFormat();
			tf1.size = 12;
			tf1.bold = true;
			tf1.font = "Arial";
			tf1.color = 0xff0000;

			var tf2:TextFormat = new TextFormat();
			tf2.size = 10;
			//tf2.bold = true;
			tf2.font = "Arial";

			var tf3:TextFormat = new TextFormat();
			tf3.size = 12;
			tf3.bold = true;
			tf3.font = "Arial";


			cc1.addItem({data:"OEM",label:"OEM原厂制造"});
			cc1.addItem({data:"Supply",label:"Supply部件供应商"});
			cc1.addItem({data:"Others",label:"Others其他"});
			cc1.textField.setStyle("textFormat", tf);
			cc1.dropdown.setRendererStyle("textFormat", tf);

			ct1.setStyle("textFormat", tf);
			ct2.setStyle("textFormat", tf);
			ct3.setStyle("textFormat", tf);
			ct4.setStyle("textFormat", tf);
			ct5.setStyle("textFormat", tf2);
			ct6.setStyle("textFormat", tf);
			ct6.addEventListener(Event.CHANGE,onChangeText);
			p00.setStyle("textFormat", tf);

			p11.setStyle("textFormat", tf);
			p11.restrict = "0-9//.";
			p11.addEventListener(Event.CHANGE,onChange);
			p12.setStyle("textFormat", tf);
			p12.restrict = "0-9//.";
			p12.addEventListener(Event.CHANGE,onChange);
			p13.setStyle("textFormat", tf);
			p13.restrict = "0-9//.";
			p13.addEventListener(Event.CHANGE,onChange);
			p14.setStyle("textFormat", tf3);
			p15.setStyle("textFormat", tf1);

			p21.setStyle("textFormat", tf);
			p21.restrict = "0-9//.";
			p21.addEventListener(Event.CHANGE,onChange);
			p22.setStyle("textFormat", tf);
			p22.restrict = "0-9//.";
			p22.addEventListener(Event.CHANGE,onChange);
			p23.setStyle("textFormat", tf);
			p23.restrict = "0-9//.";
			p23.addEventListener(Event.CHANGE,onChange);
			p24.setStyle("textFormat", tf3);
			p25.setStyle("textFormat", tf1);

			p31.setStyle("textFormat", tf);
			p31.restrict = "0-9//.";
			p31.addEventListener(Event.CHANGE,onChange);
			p32.setStyle("textFormat", tf);
			p32.restrict = "0-9//.";
			p32.addEventListener(Event.CHANGE,onChange);
			p33.setStyle("textFormat", tf);
			p33.restrict = "0-9//.";
			p33.addEventListener(Event.CHANGE,onChange);
			p34.setStyle("textFormat", tf3);
			p35.setStyle("textFormat", tf1);

			p41.setStyle("textFormat", tf);
			p41.restrict = "0-9//.";
			p41.addEventListener(Event.CHANGE,onChange);
			p42.setStyle("textFormat", tf);
			p42.restrict = "0-9//.";
			p42.addEventListener(Event.CHANGE,onChange);
			p44.setStyle("textFormat", tf3);
			p45.setStyle("textFormat", tf1);

			p51.setStyle("textFormat", tf);
			p51.restrict = "0-9//.";
			p51.addEventListener(Event.CHANGE,onChange);
			p52.setStyle("textFormat", tf);
			p52.restrict = "0-9//.";
			p52.addEventListener(Event.CHANGE,onChange);
			p54.setStyle("textFormat", tf3);
			p55.setStyle("textFormat", tf1);

			p61.setStyle("textFormat", tf);
			p61.restrict = "0-9//.";
			p61.addEventListener(Event.CHANGE,onChange);
			p62.setStyle("textFormat", tf);
			p62.restrict = "0-9//.";
			p62.addEventListener(Event.CHANGE,onChange);
			p64.setStyle("textFormat", tf3);
			p65.setStyle("textFormat", tf1);

			p71.setStyle("textFormat", tf);
			p71.restrict = "0-9//.";
			p71.addEventListener(Event.CHANGE,onChange);
			p72.setStyle("textFormat", tf);
			p72.restrict = "0-9//.";
			p72.addEventListener(Event.CHANGE,onChange);
			p74.setStyle("textFormat", tf3);
			p75.setStyle("textFormat", tf1);

			p81.setStyle("textFormat", tf);
			p81.restrict = "0-9//.";
			p81.addEventListener(Event.CHANGE,onChange);
			p82.setStyle("textFormat", tf);
			p82.restrict = "0-9//.";
			p82.addEventListener(Event.CHANGE,onChange);
			p84.setStyle("textFormat", tf3);
			p85.setStyle("textFormat", tf1);

			p91.setStyle("textFormat", tf);
			p91.restrict = "0-9//.";
			p91.addEventListener(Event.CHANGE,onChange);
			p92.setStyle("textFormat", tf);
			p92.restrict = "0-9//.";
			p92.addEventListener(Event.CHANGE,onChange);
			p90.setStyle("textFormat", tf);
			p90.restrict = "0-9//.";
			p90.addEventListener(Event.CHANGE,onChange);
			p93.setStyle("textFormat", tf);
			p93.restrict = "0-9//.";
			p93.addEventListener(Event.CHANGE,onChange);
			p941.setStyle("textFormat", tf3);
			p951.setStyle("textFormat", tf1);
			p942.setStyle("textFormat", tf3);
			p952.setStyle("textFormat", tf1);


			p04.setStyle("textFormat", tf3);
			p05.setStyle("textFormat", tf1);


			tp1.addEventListener(MouseEvent.CLICK,onSave);
			tp2.addEventListener(MouseEvent.CLICK,onPrint);

			v0.text = "";
			v1.text = "";
			v2.text = "";
			v3.text = "";
			v4.text = "";
		}

		private function round(x:Number,p:int):Number
		{
			if (p==0)
			{
				return Math.round(x);
			}
			else
			{
				return Math.round(x*Math.pow(10,p))/Math.pow(10,p);
			}
		}

		private function getVal(val:String):Number
		{
			if (isNaN(Number(val.split(",").join(""))))
			{
				return 0;
			}
			return Number(val);
		}

		private function onChangeText(e:Event):void
		{
			p00.text = ct6.text;
		}

		private function onChange(e:Event):void
		{
			var n04:Number;
			var n0401:Number;
			var n0402:Number;
			var n0403:Number;
			var n0404:Number;
			var n05:Number;
			var n0501:Number;
			var n0502:Number;
			var n0503:Number;
			var n0504:Number;
			var h:Number;
			var d:Number;

			var tf4:TextFormat = new TextFormat();
			tf4.size = 12;
			tf4.bold = true;
			tf4.font = "Arial";
			tf4.color = 0x310101;

			var tf5:TextFormat = new TextFormat();
			tf5.size = 12;
			tf5.bold = true;
			tf5.font = "Arial";
			tf5.color = 0xff0000;


			//能耗
			p14.text=styler.format(round(getVal(p11.text)*getVal(p12.text)*getVal(p13.text)*12,2),new Locale("en","US")).toString();
			n04 = getVal(p11.text) * getVal(p12.text) * getVal(p13.text) * 12;
			p15.text=styler.format(round(getVal(p11.text)*getVal(p12.text)*getVal(p13.text)*12*0.15,2),new Locale("en","US")).toString();
			n05 = n04 * 0.15;
			p24.text=styler.format(round(getVal(p21.text)*getVal(p22.text)*getVal(p23.text)*12,2),new Locale("en","US")).toString();
			n04 += getVal(p21.text)*getVal(p22.text)*getVal(p23.text)*12;
			p25.text=styler.format(round(getVal(p21.text)*getVal(p22.text)*getVal(p23.text)*12*0.15,2),new Locale("en","US")).toString();
			n05 = n04 * 0.15;
			p34.text=styler.format(round(getVal(p31.text)*getVal(p32.text)*getVal(p33.text)*12,2),new Locale("en","US")).toString();
			n04 +=  getVal(p31.text) * getVal(p32.text) * getVal(p33.text) * 12;
			p35.text=styler.format(round(getVal(p31.text)*getVal(p32.text)*getVal(p33.text)*12*0.15,2),new Locale("en","US")).toString();
			n05 =  n04 * 0.15;
			
			n0401 = n04;
			n0501 = n05;
			
			_bar1=new Sprite();
			_bar1.graphics.beginFill(0xffffff,1);
			_bar1.graphics.drawRect(0,0,40,40);
			_bar1.graphics.endFill();
			_bar1.x = 60;
			_bar1.y = 764;
			if (n0401>0)
			{
				_bar1.graphics.beginFill(0xff0000,1);
				_bar1.graphics.drawRect(20,34,15,6);
				_bar1.graphics.endFill();
				_bar1.graphics.beginFill(0x310101,1);
				_bar1.graphics.drawRect(0,0,15,40);
				_bar1.graphics.endFill();
				v1.setStyle("textFormat", tf5);
				v1.text = "-85%";
			}
			else
			{
				v1.text = "";
			}
			addChild(_bar1);
			
			//废料
			p44.text=styler.format(round(getVal(p41.text)*getVal(p42.text)*12,2),new Locale("en","US")).toString();
			n04 +=  getVal(p41.text) * getVal(p42.text) * 12;
			p45.text = "0";
			p54.text=styler.format(round(getVal(p51.text)*getVal(p52.text)*12,2),new Locale("en","US")).toString();
			n04 +=  getVal(p51.text) * getVal(p52.text) * 12;
			p55.text=styler.format(round(getVal(p51.text)*getVal(p52.text)*12*0.5,2),new Locale("en","US")).toString();
			n05 +=  getVal(p51.text) * getVal(p52.text) * 12 * 0.5;
			p64.text=styler.format(round(getVal(p61.text)*getVal(p62.text)*12,2),new Locale("en","US")).toString();
			n04 +=  getVal(p61.text) * getVal(p62.text) * 12;
			p65.text=styler.format(round(getVal(p61.text)*getVal(p62.text)*12*0.5,2),new Locale("en","US")).toString();
			n05 +=  getVal(p61.text) * getVal(p62.text) * 12 * 0.5;
			n0402 = n04 - n0401;
			n0502 = n05 - n0501;

			_bar2=new Sprite();
			_bar2.graphics.beginFill(0xffffff,1);
			_bar2.graphics.drawRect(0,0,40,40);
			_bar2.graphics.endFill();
			_bar2.x = 180;
			_bar2.y = 764;
			if (n0402>0)
			{				
				h = 0;
				_bar2.graphics.beginFill(0x310101,1);
				_bar2.graphics.drawRect(0,0,15,40);
				_bar2.graphics.endFill();
				_bar2.graphics.beginFill(0xff0000,1);
				h = int(40*n0502/n0402);
				d = round((n0502/n0402-1)*100,1);
				_bar2.graphics.drawRect(20,40-h,15,h);
				_bar2.graphics.endFill();
				v2.setStyle("textFormat", tf5);
				v2.text = d.toString() + "%";
			}
			else
			{
				v2.text = "";
			}
			addChild(_bar2);
			//化学品
			p74.text=styler.format(round(getVal(p71.text)*getVal(p72.text)*12,2),new Locale("en","US")).toString();
			n04 +=  getVal(p71.text) * getVal(p72.text) * 12;
			p75.text=styler.format(round(getVal(p71.text)*getVal(p72.text)*12*1.5,2),new Locale("en","US")).toString();
			n05 +=  getVal(p71.text) * getVal(p72.text) * 12 * 1.5;

			p84.text=styler.format(round(getVal(p81.text)*getVal(p82.text)*12,2),new Locale("en","US")).toString();
			n04 +=  getVal(p81.text) * getVal(p82.text) * 12;
			p85.text=styler.format(round(getVal(p81.text)*getVal(p82.text)*12*1.3,2),new Locale("en","US")).toString();
			n05 +=  getVal(p81.text) * getVal(p82.text) * 12 * 1.3;

			n0403 = n04 - n0401 - n0402;
			n0503 = n05 - n0501 - n0502;
			
			_bar3=new Sprite();
			_bar3.graphics.beginFill(0xffffff,1);
			_bar3.graphics.drawRect(0,0,40,40);
			_bar3.graphics.endFill();
			_bar3.x = 60;
			_bar3.y = 824;
			if (n0503>0)
			{				
				h = 0;
				_bar3.graphics.beginFill(0x310101,1);
				h=int(40*n0403/n0503);
				d = round((n0503/n0403-1)*100,1);
				_bar3.graphics.drawRect(0,40-h,15,h);
				_bar3.graphics.endFill();
				_bar3.graphics.beginFill(0xff0000,1);
				_bar3.graphics.drawRect(20,0,15,40);
				_bar3.graphics.endFill();
				v3.setStyle("textFormat", tf4);
				v3.text = "+" + d.toString() + "%";
			}
			else
			{
				v3.text = "";
			}
			addChild(_bar3);
			
			p941.text=styler.format(round(getVal(p91.text)*(1-getVal(p92.text))*getVal(p90.text)*getVal(p93.text)*getVal(p71.text)*12,2),new Locale("en","US")).toString();
			n04 +=  getVal(p91.text) * (1 - getVal(p92.text)) * getVal(p90.text) * getVal(p93.text) * getVal(p71.text) * 12;
			p951.text = "0";
			p942.text=styler.format(round(getVal(p91.text)*(1-getVal(p92.text))*getVal(p90.text)*getVal(p93.text)*getVal(p81.text)*12,2),new Locale("en","US")).toString();
			n04 +=  getVal(p91.text) * (1 - getVal(p92.text)) * getVal(p90.text) * getVal(p93.text) * getVal(p81.text) * 12;
			p952.text = "0";

			n0404 = n04 - n0401 - n0402 - n0403;
			n0504 = n05 - n0501 - n0502 - n0503;

			_bar4=new Sprite();
			_bar4.graphics.beginFill(0xffffff,1);
			_bar4.graphics.drawRect(0,0,40,40);
			_bar4.graphics.endFill();
			_bar4.x = 180;
			_bar4.y = 824;
			if (n0404>0)
			{
				_bar4.graphics.beginFill(0x310101,1);
				_bar4.graphics.drawRect(0,0,15,40);
				_bar4.graphics.endFill();
				_bar4.graphics.beginFill(0xff0000,1);
				_bar4.graphics.drawRect(20,38,15,1);
				_bar4.graphics.endFill();				
				v4.setStyle("textFormat", tf5);
				v4.text = "-100%";
			}
			else
			{
				v4.text = "";
			}
			addChild(_bar4);
			
			p04.text = styler.format(round(n04,2),new Locale("en","US")).toString();
			p05.text = styler.format(round(n05,2),new Locale("en","US")).toString();

			_bar0=new Sprite();
			_bar0.graphics.beginFill(0xffffff,1);
			_bar0.graphics.drawRect(0,0,80,80);
			_bar0.graphics.endFill();
			_bar0.x = 470;
			_bar0.y = 780;
			if (n04>0)
			{
				
				if (n04>=n05)
				{
					_bar0.graphics.beginFill(0x310101,1);
					_bar0.graphics.drawRect(0,0,20,80);
					_bar0.graphics.endFill();
					h = 0;
					h=int(80*n05/n04);
					_bar0.graphics.beginFill(0xff0000,1);
					_bar0.graphics.drawRect(25,79-h,20,h);
					_bar0.graphics.endFill();
				}
				else
				{
					h = 0;
					h=int(80*n04/n05);
					_bar0.graphics.beginFill(0x310101,1);
					_bar0.graphics.drawRect(0,79-h,20,h);
					_bar0.graphics.endFill();
					_bar0.graphics.beginFill(0xff0000,1);
					_bar0.graphics.drawRect(25,0,20,80);
					_bar0.graphics.endFill();
				}
				d = round((n05/n04-1)*100,1);				
				if (d<0)
				{
					v0.setStyle("textFormat", tf5);
					v0.text =d.toString()+"%";
				}else{
					v0.setStyle("textFormat", tf4);
					v0.text = "+"+d.toString()+"%";
				}
			}
			else
			{
				v0.text = "";
			}
			addChild(_bar0);
			//p14.text="1111";
			/*
			if (isNaN(p11.text.split(",").join("")) 
			&& isNaN(p12.text.split(",").join("")) 
			&& isNaN(p13.text.split(",").join(""))
			   ){
			
			p14.text=styler.format(round(p11.text*p12.text*p13.text*12,0),new Locale("en","US")).toString();
			    p15.text=styler.format(round(p11.text*p12.text*p13.text*12*0.15,0),new Locale("en","US")).toString();
			}
			*/

		}

		private function onFormatData(e:FocusEvent):void
		{
			if (isNaN(e.currentTarget.text.split(",").join("")))
			{
				e.currentTarget.text = "0";
			}
			e.currentTarget.text = styler.format(Number(e.currentTarget.text.split(",").join("")),new Locale("en","US"));
			onChange(e);
		}

		private function onChangeData(e:FocusEvent):void
		{
			e.currentTarget.text = e.currentTarget.text.split(",").join("");
		}

		private function onPrint(e:MouseEvent):void
		{
			var printJob:PrintJob=new PrintJob();
			var printArea:Rectangle;
			if (! printJob.start())
			{
				return;
			}
			var bit:BitmapData = new BitmapData(this.width,this.height - 20);
			var mat:Matrix=new Matrix();
			mat.scale(1,1);
			bit.draw(this,mat);
			var bmap:Bitmap = new Bitmap(bit);
			var s:Sprite=new Sprite();
			s.addChild(bmap);
			s.width = printJob.pageWidth;
			s.scaleY = s.scaleX;
			printJob.addPage(s, printArea);
			printArea = new Rectangle(0,0,printJob.pageWidth,printJob.pageHeight);
			printJob.send();
			/* Delete job from memory */
			printJob = null;

		}

		private function onSave(e:MouseEvent):void
		{
			var bit:BitmapData = new BitmapData(this.width,this.height - 20);
			var mat:Matrix=new Matrix();
			mat.scale(1,1);
			bit.draw(this,mat);
			var bmap:Bitmap = new Bitmap(bit);
			var s:Sprite=new Sprite();
			s.addChild(bmap);
			var jpg:JPGEncoder = new JPGEncoder(100);
			var file:FileReference = new FileReference();
			//file.save(jpg.encode(bit),"henkel"+(new Date().fullYear*10000+(new Date().month+1)*100+new Date().date).toString()+".jpg");
			var myPDF:PDF = new PDF(Orientation.PORTRAIT,Unit.POINT);
			myPDF.addPage();
			myPDF.addImage(s, new Resize ( Mode.FIT_TO_PAGE, Position.CENTERED ));
			file.save(myPDF.save(Method.LOCAL ),"henkel-st"+(new Date().fullYear*10000+(new Date().month+1)*100+new Date().date).toString()+".pdf");
		}

	}

}